package com.company.cloud.product.modules.mgr.controller;

import java.util.function.Function;

import com.company.cloud.common.auth.annotation.StaffLogc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.log.annotation.SysLog;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.product.constant.ProductErrorEnum;
import com.company.cloud.product.modules.base.entity.BrandInfo;
import com.company.cloud.product.modules.base.service.BrandInfoService;
import com.company.cloud.product.modules.cache.service.ProductConfigCacheService;
import com.company.cloud.product.modules.cache.utils.ProductCacheConvertUtils;
import com.company.cloud.product.modules.mgr.controller.req.BrandInfoQuery4PageParam;
import com.company.cloud.product.modules.mgr.controller.req.BrandInfoSaveParam;
import com.company.cloud.product.modules.mgr.controller.req.BrandInfoUpdateParam;
import com.company.cloud.product.modules.mgr.controller.resp.BrandInfoModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 商品品牌管理
 */
@Authc
@RestController
@RequestMapping("/mgr/prd/brand")
@Api(tags="Product_Mgr_商品品牌管理")
public class BrandMgrController {

	@Autowired
	private BrandInfoService brandInfoService;
	@Autowired
	private ProductConfigCacheService productCacheService;
	
	@PostMapping("/query")
	@SysLog("商品品牌表列表查询")
	@ApiOperation(value="商品品牌表列表查询", response=BaseResult.class)
	public BaseResult query(@RequestBody BrandInfoQuery4PageParam params) {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		IPage<BrandInfo> page = brandInfoService.page(
				new Page<>(params.getPage(), params.getRows()), 
					new QueryWrapper<BrandInfo>()
					.eq("company_no", userInfo.getCompanyNo())
					.eq(CheckUtils.hasText(params.getType()), "type", params.getType())
					.like(CheckUtils.hasText(params.getName()), "name", params.getName())
					);
		IPage<BrandInfoModel> data = page.convert(new Function<BrandInfo, BrandInfoModel>() {
			@Override
			public BrandInfoModel apply(BrandInfo entity) {
				return ProductCacheConvertUtils.convertProductBrand(entity);
			}
		});
		return BaseResult.ok(data);
	}
	

	
	@GetMapping("/get")
	@SysLog("商品品牌表详情")
	@ApiOperation(value="商品品牌表详情查询")
	public BaseResult get(@RequestParam("id")Integer id) {
		BrandInfo entity= brandInfoService.getById(id);
		if(entity==null) {
			throw new BusinessException(ProductErrorEnum.Product_Brand_NULL);
		}
		BrandInfoModel data= ProductCacheConvertUtils.convertProductBrand(entity);
		return BaseResult.ok(data);
	}
	
	@PostMapping("/save")
	@SysLog("商品品牌表新增")
	@ApiOperation(value="商品品牌表新增")
	@StaffLogc(title = "商品品牌表新增", action = "商品品牌表新增", actionAdo = "商品品牌表")
	public BaseResult save(@RequestBody BrandInfoSaveParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		BrandInfo entity=new BrandInfo();
		entity.setCompanyNo(userInfo.getCompanyNo());
		entity.setLangType(AuthContextUtils.getLang());
		entity.setName(params.getName());
		entity.setAliasName(params.getAliasName());
		entity.setType(params.getType());
		entity.setLogo(params.getLogo());
		entity.setUrl(params.getUrl());
		entity.setIntroduction(params.getIntroduction());
		entity.setOrders(params.getOrders()==null?0:params.getOrders());
		entity.addCreateParam(userInfo);
		brandInfoService.save(entity);
		productCacheService.reloadBrandByCache(entity.getCompanyNo(), entity.getId());
		return BaseResult.ok();
	}
	
	@PostMapping("/update")
	@SysLog("商品品牌表修改")
	@ApiOperation(value="商品品牌表修改")
	@StaffLogc(title = "商品品牌表修改", action = "商品品牌表修改", actionAdo = "商品品牌表")
	public BaseResult update(@RequestBody BrandInfoUpdateParam params) {
		BrandInfo entity= brandInfoService.getById(params.getId());
		if(entity==null) {
			throw new BusinessException(ProductErrorEnum.Product_Brand_NULL);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setName(params.getName());
		entity.setAliasName(params.getAliasName());
		entity.setType(params.getType());
		entity.setLogo(params.getLogo());
		entity.setUrl(params.getUrl());
		entity.setIntroduction(params.getIntroduction());
		entity.setOrders(params.getOrders()==null?0:params.getOrders());
		entity.addEditParam(userInfo);
		brandInfoService.updateById(entity);
		productCacheService.reloadBrandByCache(entity.getCompanyNo(), entity.getId());
		return BaseResult.ok();
	}
	
	@GetMapping("/delete")
	@SysLog("商品品牌表删除")
	@ApiOperation(value="商品品牌表删除")
	@StaffLogc(title = "商品品牌表删除", action = "商品品牌表删除", actionAdo = "商品品牌表")
	public BaseResult delete(@RequestParam("id")Integer id) {
		BrandInfo entity= brandInfoService.getById(id);
		if(entity==null) {
			throw new BusinessException(ProductErrorEnum.Product_Brand_NULL);
		}
		//判断引用品牌的表
		brandInfoService.removeById(id);
		productCacheService.removeBrandByCache(entity.getCompanyNo(), entity.getId());
		
		return BaseResult.ok();
	}
}
